package com.apache.bid.controller.admin;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.bid.controller.BaseSupperAction;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.manager.BidItemPriceManager;
import com.apache.bid.manager.UnityBaseManager;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.tools.StrUtil;

@Controller
@RequestMapping("/admin/price")
public class BidItemPriceAction extends BaseSupperAction<BidItemPrice> {

	@Autowired
	private BidItemPriceManager bidItemPriceManager;
	@Autowired
	private ProxyManager wbdProxyManager;

	@Override
	protected Object listData(HttpServletRequest request, BidItemPrice item, String page, String rows) {
		Map<String, Object> mapData = new HashMap<String, Object>();
		String sessionId = Validator.getDefaultStr(request.getParameter("sessionId"), "0");
		String itemName = Validator.getDefaultStr(request.getParameter("itemName"), "");
		/*try {
			itemName = new String(itemName.getBytes("iso-8859-1"), "UTF-8");//处理request请求中文乱码
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		if (StrUtil.isNotNull(itemName)) {
			try {
				if (itemName.equals(new String(itemName.getBytes("iso8859-1"), "iso8859-1"))) {
					itemName = new String(itemName.getBytes("iso8859-1"), "utf-8");
				}
			} catch (UnsupportedEncodingException e) {
			}
		}
		ParamsVo<BidItemPrice> ivo = new ParamsVo<BidItemPrice>();
		ivo.setParams("sessionId", sessionId);
		//ivo.setParams("itemStatus", Constant.getInstance().getSessionConfig("statusPubslish"));//已发布的标的
		ivo.setParams("itemStatus", "i.item_status <> '0'");
		String currentTimePre = request.getParameter("currentTimePre");
		if (Validator.isNotNull(currentTimePre)) {
			BidItemPrice price = new BidItemPrice();
			price.setOpenTime(currentTimePre);
			ivo.setObj(price);
			ivo.setMethodKey("BidItemPriceByIdVo");
			ivo.setParams("itemName", itemName);
		}

		if (Validator.isNotNull(rows)) {//rows为空表示不进行分页处理
			ivo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
			ivo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
			Page pageList = bidItemPriceManager.getPageInfo(ivo);//分页
			mapData.put("total", pageList.getCount());//dataGrid所需参数
			mapData.put("rows", pageList.getPageObjects());//dataGrid所需数据对象集合
		} else {
			List list = bidItemPriceManager.getList(ivo);
			if (!Validator.isEmpty(list)) {
				for (Object object : list) {
					System.out.println(JSONObject.fromObject(object));
				}
				mapData.put("total", list.size());//dataGrid所需参数
				mapData.put("rows", list);//dataGrid所需数据对象集合
			}
		}
		return mapData;
	}

	@Override
	protected UnityBaseManager<BidItemPrice> getBaseManager() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getCommBoxValue(BidItemPrice item, String type) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setBidItemPriceManager(BidItemPriceManager bidItemPriceManager) {
		this.bidItemPriceManager = bidItemPriceManager;
	}

	public void setWbdProxyManager(ProxyManager wbdProxyManager) {
		this.wbdProxyManager = wbdProxyManager;
	}

}
